<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn 2014年8月27日16:58:00
 * @小组应用
 */
namespace IKPHP\Apps\Group\Controllers;

use IKPHP\Apps\Group\Controllers;
use IKPHP\Common\Models\User;
use IKPHP\Apps\Space\Models\UserFollow;
use IKPHP\Apps\Group\Models\Group;
use IKPHP\Apps\Group\Models\GroupCate;
use IKPHP\Apps\Group\Models\GroupUsers;
use IKPHP\Apps\Group\Models\GroupTopics;

use IKPHP\Common\Models\LikeModel;
use IKPHP\Common\Models\Recommend;

class PeopleController extends GroupBaseController
{
	private $userid = 0;
	private $preuserid = 0;
	
    public function initialize()
    {
        parent::initialize();
        // 访问者控制
		if ($this->visitor){
			$this->userid = $this->visitor['userid'];
		}        
    }

    /**
     * 统一路口
     * @param string $id 用户doname
     * */
    protected function display($id, $isActive = false){
		if(empty($id)) return $this->error('呃...你想访问的页面不存在');
    	//根据ID获取uid; 判断是否是uid 还是doname
		$userid = $this->isValidUser($id); 
		$this->preuserid = $userid ;	
		$strUser = User::getOneUser ( $userid ); 		
		if(empty($strUser)) return $this->error ( '呃...你想要的东西不在这儿' );
    	//个人访问控制 非自己无法访问
		if($isActive){
			if($this->userid != $userid){$this->error('呃...你没有权限访问该页面');}
		}
		
		
		$strUser['isfollow'] = UserFollow::isFollow($this->userid, $userid);
		$this->assign ( 'strUser', $strUser );
		$title = $this->userid==$strUser['userid'] ? '我的小组主页':$strUser['username'].'的小组主页'; 	
		$this->_config_seo ( array (
				'title' => $title,
				'subtitle'=>'小组_'.C('ik_site_title'),
				'keywords' =>'',
				'description'=>'',
		) );     	
    }
    //发现首页
    public function indexAction($id = '')
    {
    	//赋值模板
 		$this->display($id);
 		
 		$userid = $this->preuserid;
 		
        //我常去的小组 加入的小组
		$myJoinGroup = Group::getUserJoinGroup( $userid );
		if(is_array($myJoinGroup)){
			$arrMyGroup = array();
			foreach($myJoinGroup as $key=>$item){
				$arrMyGroup[] = Group::getOneGroup($item['groupid']);
			}
			$this->assign('arrMyGroup',$arrMyGroup);//我加入和管理的小组
		}
		
		//发起的话题
 		$arrPublish = array();
 		$arrTopics = GroupTopics::getPublishTopics($userid,20);
		foreach($arrTopics as $key=>$item){
			$arrPublish[] = $item;
			$arrPublish[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrPublish',$arrPublish);

		//推荐的话题
 		$arrRecom = array();
 		$arrTopics = GroupTopics::getRecomTopics($userid,20);
		foreach($arrTopics as $key=>$item){
			$arrRecom[] = $item;
			$arrRecom[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrRecom',$arrRecom); 	

		//喜欢的话题
 		$arrLike = array();
 		$arrTopics = GroupTopics::getLikeTopics($userid,20);
		foreach($arrTopics as $key=>$item){
			$arrLike[] = $item;
			$arrLike[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrLike',$arrLike); 

		//回应的话题
 		$arrReplty = array();
 		$arrTopics = GroupTopics::getReplyTopics($userid,20);
		foreach($arrTopics as $key=>$item){
			$arrReplty[] = $item;
			$arrReplty[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrReplty',$arrReplty); 			
 		
    }
    
    //加入的小组
    public function joinsAction($id=''){
    	//赋值模板
 		$this->display($id); 
     		
 		$userid = $this->preuserid;
 		
        //我常去的小组 加入的小组
		$myJoinGroup = Group::getUserJoinGroup( $userid );
		if(is_array($myJoinGroup)){
			foreach($myJoinGroup as $key=>$item){
				$arrMyGroup[] = Group::getOneGroup($item['groupid']);
			}
			$this->assign('arrMyGroup',$arrMyGroup);//加入的小组
		}
		//某用户创建的小组
		$myGroup = Group::getUserGroup($userid); 	
		$this->assign('myGroup',$myGroup);//管理的小组	 
    }
    //喜欢的
    public function likesAction($id=''){
    	//赋值模板
 		$this->display($id);	
 		$userid = $this->preuserid;
 		
 		$arrLike = array();
 		$arrTopics = GroupTopics::getLikeTopics($userid,80);
		foreach($arrTopics as $key=>$item){
			$arrLike[] = $item;
			$arrLike[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrLike',$arrLike); 	 			
    }
    //推荐的
    public function recommendAction($id=''){
    	//赋值模板
 		$this->display($id);	
 		$userid = $this->preuserid;
 		
 		$arrRecom = array();
 		$arrTopics = GroupTopics::getRecomTopics($userid,80);
		foreach($arrTopics as $key=>$item){
			$arrRecom[] = $item;
			$arrRecom[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrRecom',$arrRecom); 	  				
    }
    //发表的
    public function publishAction($id=''){
    	//赋值模板
 		$this->display($id, true);		

 		$userid = $this->preuserid;
 		
 		$arrPublish = array();
 		$arrTopics = GroupTopics::getPublishTopics($userid,80);
		foreach($arrTopics as $key=>$item){
			$arrPublish[] = $item;
			$arrPublish[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrPublish',$arrPublish);
 		
    }
    //回应的
    public function replyAction($id=''){
    	//赋值模板
 		$this->display($id, true);	

 		$userid = $this->preuserid;
 		
 		$arrReplty = array();
 		$arrTopics = GroupTopics::getReplyTopics($userid,80);
		foreach($arrTopics as $key=>$item){
			$arrReplty[] = $item;
			$arrReplty[$key]['group'] = Group::getOneGroup($item['groupid']);
		} 	
		$this->assign('arrReplty',$arrReplty); 		
    }    

}